package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.MeasureConverter;
import com.youlai.system.mapper.SysMeasureMapper;
import com.youlai.system.model.entity.SysMeasure;
import com.youlai.system.model.form.MeasureForm;
import com.youlai.system.model.query.MeasurePageQuery;
import com.youlai.system.model.vo.MeasurePageVO;
import com.youlai.system.service.SysMeasureService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 整改措施业务处理实现
 */
@Service
@RequiredArgsConstructor
public class SysMeasureServiceImpl extends ServiceImpl<SysMeasureMapper, SysMeasure> implements SysMeasureService {

    private final MeasureConverter measureConverter;

    @Override
    public SysMeasure getByContentEqual(String content) {
        LambdaQueryWrapper<SysMeasure> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMeasure::getContent, content);
        return getOne(queryWrapper);
    }

    @Override
    public SysMeasure saveByContent(String content) {
        SysMeasure measure = new SysMeasure();
        measure.setContent(content);
        save(measure);
        return measure;
    }

    @Override
    public Page<MeasurePageVO> getMeasurePage(MeasurePageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        LambdaQueryWrapper<SysMeasure> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(keywords), SysMeasure::getContent, keywords);
        //查询数据
        Page<SysMeasure> categoryPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        return measureConverter.entity2Page(categoryPage);
    }

    @Override
    public boolean saveMeasure(MeasureForm measureForm) {
        String content = measureForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysMeasure>().eq(SysMeasure::getContent, content));
        Assert.isTrue(count == 0, "整改措施已存在");

        // 实体转换
        SysMeasure measure = measureConverter.form2Entity(measureForm);
        return save(measure);
    }

    @Override
    public boolean updateMeasure(Long measureId, MeasureForm measureForm) {
        String content = measureForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysMeasure>()
                .eq(SysMeasure::getContent, content)
                .ne(SysMeasure::getId, measureId)
        );
        Assert.isTrue(count == 0, "整改措施已存在");

        // form -> entity
        SysMeasure entity = measureConverter.form2Entity(measureForm);

        // 修改分类
        return this.updateById(entity);
    }

    @Override
    public MeasureForm getMeasureForm(Long measureId) {
        SysMeasure entity = this.getById(measureId);
        return measureConverter.entity2Form(entity);
    }

    @Override
    public boolean deleteMeasures(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "整改措施删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteMeasures(List<Long> idList) {
        return this.removeByIds(idList);
    }

    @Override
    public List<Option> listMeasureOptions() {
        // 查询数据
        List<SysMeasure> measureList = this.list(new LambdaQueryWrapper<SysMeasure>()
                .select(SysMeasure::getId, SysMeasure::getContent)
                .orderByAsc(SysMeasure::getSort)
        );

        // 实体转换
        return measureConverter.entities2Options(measureList);
    }
}
